import AbsControler from '../abscontroler.js'
import globalConst from '../../common/globalConst.js';
var global = globalConst.GlobalConst;
import { GlobalVar } from '../../common/globalvar.js';
import {CoordTRFUtil} from '../../common/tools/coordtool.js';

class ZoomViewControler extends AbsControler.AbsControler {
    constructor(datamanger) {
        super(datamanger);
        this.curentScaling = 1;
    }

    act2MouseWheel(delta, screenPt,ctrlKeyDown) {
        if (GlobalVar.easySelMode && !ctrlKeyDown) {
            this.pan2MouseWheel(delta,screenPt);
        }
        else {
            var oldScaling = this.dataManger.getScaling();
            var scaling = oldScaling;
            var scaleSped = 1.1;
            if (delta > 0) {
                scaling = scaling * scaleSped;
            }
            else if (delta < 0) {
                scaling = scaling / scaleSped;
            }
            var mapInfo = this.dataManger.getMapViewInfo();
            this.zoomTo(scaling, oldScaling,screenPt, mapInfo);
        }
    }

    pan2MouseWheel(delta, screenPt) {
        var mapInfo = this.dataManger.getMapViewInfo();
        var viewPortRect = mapInfo.getViewPort();
        const offsetgap = 20;
        if (delta > 0) {
            viewPortRect.top += offsetgap;
            viewPortRect.bottom += offsetgap;
        }
        else if (delta < 0) {
            viewPortRect.top -= offsetgap;
            viewPortRect.bottom -= offsetgap;
        }
        mapInfo.setViewPort(viewPortRect.left, viewPortRect.top, viewPortRect.right, viewPortRect.bottom);
    }

    zoomTo(curentScaling, oldScaling,screenPt, mapInfo) {
        if (curentScaling < global.MinDisScaling) {
            curentScaling = global.MinDisScaling;
        }
        else if (curentScaling > global.MaxDisScaling) {
            curentScaling = global.MaxDisScaling;
        }

        let mapHgt = mapInfo.getMapHeight();
        let mapWid = mapInfo.getMapWidth();
        let projectScale = mapInfo.getProjectScale();
        let oldScale = projectScale/oldScaling;
        let scale = projectScale/curentScaling;
        let wroldPt = CoordTRFUtil.screen2world(screenPt,mapInfo);

        let oldViewPort = mapInfo.getViewPort();
        //wroldPt.x = pt.x*oldScale + oldViewPort.left = pt.x*scale + newTransX;
        //wroldPt.y = (mapHgt - pt.y)*oldScale + oldViewPort.bottom = (mapHgt - pt.y)*scale + newTransY;

        let newTransX = wroldPt.x - screenPt.x*scale;
        let newTransY = wroldPt.y - (mapHgt - screenPt.y)*scale;

        var worldMapRect = mapInfo.getWorldMapRect();
        var nWid = mapWid*scale;
        var nHgh = mapHgt*scale;

        var newRight = newTransX + nWid;
        var newTop = newTransY + nHgh;

        mapInfo.setViewPort(newTransX, newTop, newRight, newTransY);
        mapInfo.setScaling(curentScaling);
    }
}

export default { ZoomViewControler }

